import pathlib

from kivy.properties import StringProperty, NumericProperty, OptionProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior

from common import KvLoad
from common.date_tools import sec_to_hms
from ui.audio_player.base_weight import PlayBase


class PlayGrid(RecycleDataViewBehavior, BoxLayout, PlayBase):
    track_type = OptionProperty('interact', options=['interact'])  # 当前音轨类型 互动音轨
    index = NumericProperty()
    text_current_music_play_time = StringProperty('00:00:00')
    uuid = StringProperty()

    def __init__(self, **kwargs):
        KvLoad.load(__file__)
        super().__init__(**kwargs)
        self.ids.id_play_slider.bind(on_touch_down=self.on_slider_touch_down)
        self.ids.id_play_slider.bind(on_touch_up=self.on_slider_touch_up)
        self.on_clear_audio_update_ui()

    def on_sound_load(self):
        self.ids.id_file_name_text.text = pathlib.Path(self.sound.source).stem
        self.ids.id_music_length_text.text = sec_to_hms(self.sound.length)
        self.ids.id_play_slider.max = int(self.sound.length)
        self.ids.id_state_icon.source = self.y_icon

    def on_updater_ui(self):
        if not self.auto_move_slider:
            self.ids.id_play_slider.set_value(self.seek_value)
        self.text_current_music_play_time = sec_to_hms(self.sound.get_pos())

    def on_pause_or_proceed_update_ui(self):
        if self.play_state == 'pause':
            self.ids.id_pause_or_proceed_button.source = self.play_icon
        else:
            self.ids.id_pause_or_proceed_button.source = self.pause_icon
        self.ids.id_pause_or_proceed_button.size = 30, 30

    def on_clear_audio_update_ui(self):
        self.ids.id_state_icon.source = self.x_icon
        self.ids.id_play_slider.set_value(self.seek_value)
        self.ids.id_file_name_text.text = ''
        self.ids.id_music_length_text.text = '00:00:00'
        self.text_current_music_play_time = '00:00:00'
        self.ids.id_pause_or_proceed_button.source = self.play_icon
        self.ids.id_pause_or_proceed_button.size = 30, 30

    def on_play_completed_update_ui(self):
        self.ids.id_play_slider.set_value(self.seek_value)
        self.text_current_music_play_time = '00:00:00'
        self.ids.id_pause_or_proceed_button.source = self.play_icon
        self.ids.id_pause_or_proceed_button.size = 30, 30

    def on_close_self(self, close_self_button):
        if close_self_button.opacity == 0 or (self.sound and self.is_playing()):
            return
        self.parent.parent.parent.grid_on_dismiss_by_uuid(self.uuid)

    def refresh_view_attrs(self, rv, index, data):
        self.index = index
        self.file_path = data['file_path']
        self.group = data['group']
        self.uuid = data['uuid']
        self.on_clear_audio_update_ui()
        if self.file_path and pathlib.Path(self.file_path).is_file():
            self.init_slider()

        super(PlayGrid, self).refresh_view_attrs(rv, index, data)

    def on_file_path(self, widget, value):
        if self.uuid and self.parent and value:
            self.parent.parent.parent.parent.current_tab.grid_on_file_path(self.uuid, self.file_path)
